package com.baizhi.service.impl;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.service.UserService;
import com.baizhi.service.UserThreadService;
import com.baizhi.util.ThreadPollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

@Service
public class UserThreadServiceImpl implements UserThreadService {
    @Autowired
    private UserService userService;




    @Override
    @AddCacheAnnotation
    public Map selectAllCountThread() throws InterruptedException {
       Map map=new HashMap<>();
       //1.创建计数器
        CountDownLatch countDownLatch = new CountDownLatch(3);
        //2.开启三个任务线程
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
               try{
                   Map map1 = userService.selectByDayCount();
                   map.put("count",map1);
                   //计数器减1
                   countDownLatch.countDown();
               }catch (Exception e){

               }

            }
        });
       ThreadPollUtil.executor.execute(new Runnable() {
           @Override
           public void run() {
               //查询男女比列
               List<Map> maps = userService.selectBySex();
               map.put("sex",maps);
               //  计数器减1
               countDownLatch.countDown();
           }
       });
       ThreadPollUtil.executor.execute(new Runnable() {
           @Override
           public void run() {
               List<Map> maps = userService.selectByProvince();
               map.put("province",maps);
               //计数器减1
               countDownLatch.countDown();
           }
       });
       //3.使用await()方法 让主线程等待任务线程执行结束
        countDownLatch.await();
        return map;
    }

    @Override
    @AddCacheAnnotation
    public Map selectAllCountFuture() throws Exception {
        Map map = new HashMap<>();

        Future<String> future1 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Map map1 = userService.selectByDayCount();
                map.put("count",map1);
//                计数器减一
                return "haha";
            }
        });

        Future<String> future2 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> maps = userService.selectBySex();
                map.put("sex",maps);
                return "haha";
            }
        });

        Future<String> future3 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> maps = userService.selectByProvince();
                map.put("province",maps);
                return "haha";
            }
        });

        if ("haha".equals(future1.get()) && "haha".equals(future2.get()) && "haha".equals(future3.get())){
            return map;
        }

        return map;
    }

    /**
     * 没有优化
     * 串行执行
     * @return
     */
    @Override
    @AddCacheAnnotation
    public Map selectAllCount() {

        Map map=new HashMap<>();
        //注册量
        Map map1 = userService.selectByDayCount();
        //地区分布
        List<Map> maps = userService.selectByProvince();
        //男女数量
        List<Map> maps1 = userService.selectBySex();
        map.put("sex",maps1);
        map.put("count",map1);
        map.put("province",maps);

        return map;
    }
}
